from openai import OpenAI
import os
from dotenv import load_dotenv
load_dotenv()

client = OpenAI(
    base_url="https://ai.gitee.com/v1",
    api_key = os.getenv("api_key"),
)

response = client.moderations.create(
    input=[
        {
            "type": "image_url",
            "image_url": {
                "url": "https://example.com/image.jpg"  # 替换为实际图片URL
            }
        }
    ],
    model="nonescape-v0",
    extra_body={"threshold": 0.8},
)

try:
    result = response.results[0]
    
    category = "aigc"
    confidence = result.category_scores.aigc
    is_ai = result.categories.aigc
    
    print("AI Image Detection Result:")
    print(f"Category: {category}")
    print(f"Confidence: {confidence:.4f}")
    print(f"AI-generated image:{'Yes' if is_ai else 'No'}")

except Exception as e:
    print(f"Error parsing result:{str(e)}")
    print("Raw response data:", response)